package com.study.system.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.system.order.config.TimeoutOrderMQConfig;
import com.study.system.order.entity.UserCourseOrder;
import com.study.system.order.service.OrderService;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 订单控制器(添加订单（状态未支付）、根据交易号查询订单、
 * 根据用户id查询订单、更新订单支付状态（status 0未支付 1已支付 -1 已取消)、
 * 删除订单（逻辑删除、is_del设置1）)
 */
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 课程下单
     */
    @RequestMapping("/makeOrder")
    public ResponseEntity<String> makeOrder(Long userId,Long courseId,Long activityId,Long price){
        UserCourseOrder order = new UserCourseOrder();
        order.setActivityId(activityId);
        order.setUserId(userId);
        order.setCourseId(courseId);
        order.setPrice(price);
        order.setTradeNo(UUID.randomUUID().toString().replace("-",""));
        orderService.save(order);
        //发送超时订单消息，将在15秒后确认支付状态
        rabbitTemplate.convertAndSend(TimeoutOrderMQConfig.ORDER_EXCHANGE,
                TimeoutOrderMQConfig.ORDER_QUEUE_KEY,order.getTradeNo(),
                message -> {
                    message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                    //给消息设置超时时间
                    message.getMessageProperties().setExpiration(String.valueOf(15 * 1000));
                    return message;
                });
        return ResponseEntity.ok("下单成功");
    }

    /**
     * 根据交易号查询订单
     * @param tradeNo
     * @return
     */
    @RequestMapping("/getOrderByTradeNo")
    public ResponseEntity<UserCourseOrder> getOrderByTradeNo(String tradeNo){
        UserCourseOrder order = orderService.getOne(
                new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getTradeNo, tradeNo));
        return ResponseEntity.ok(order);
    }

    /**
     * 根据用户id查询订单
     * @return
     */
    @RequestMapping("/getOrdersByUserid")
    public ResponseEntity<List<UserCourseOrder>> getOrdersByUserid(Long userId){
        List<UserCourseOrder> order = orderService.list(
                new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getUserId, userId));
        return ResponseEntity.ok(order);
    }

    @RequestMapping("/updateOrderStatus")
    public ResponseEntity<String> updateOrderStatus(String tradeNo,Long status){
        //先查询订单信息
        UserCourseOrder order = orderService.getOne(
                new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getTradeNo, tradeNo));
        //修改订单状态
        order.setStatus(status);
        orderService.update(order, new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getTradeNo, tradeNo));
        return ResponseEntity.ok("更新成功");
    }

    @RequestMapping("/deleteOrder")
    public ResponseEntity<String> deleteOrder(String tradeNo){
        //先查询订单信息
        UserCourseOrder order = orderService.getOne(
                new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getTradeNo, tradeNo));
        //修改删除状态
        order.setIsDel(1);
        orderService.update(order, new QueryWrapper<UserCourseOrder>().lambda().eq(UserCourseOrder::getTradeNo, tradeNo));
        return ResponseEntity.ok("删除成功");
    }
}
